﻿package mortal.game.view.rebate.dayRecharge
{
    import Message.Game.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.utils.*;
    import mortal.game.view.FirstRechargeDay500.*;
    import mortal.game.view.FirstRechargeDay500.rechargeDaily.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class RebateRechargeController extends Controller
    {
        private var _rebateModule:RebateRechargeModule;

        public function RebateRechargeController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            EventUtil.addSelfEvent(EventName.RebateDayRechargeOpenModule, this.onOpenRechargeModule, true);
            EventUtil.addNetEvent(ServerCommand.FirstRechargeUpdate, this.updateFirstRechargeInfo, true);
            EventUtil.addSelfEvent(EventName.RebateDayRechargeGetReward, this.onDayRechargeGetHd, true);
            EventUtil.addNetEvent(ServerCommand.RebateDayChargeGetRewardBack, this.onGetDayRechargeBackHd, true);
            EventUtil.addNetEvent(ServerCommand.FirstRechargeRewardHasGet, this.updateFirstRechargeInfo, true);
            EventUtil.addSelfEvent(EventName.RebateShowRewardIconEffect, this.updateFirstRechargeInfo, true);
            NetDispatcher.addCmdListener(ServerCommand.NewDayUpdate, this.onDateChange);
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._rebateModule == null)
            {
                this._rebateModule = new RebateRechargeModule();
                this._rebateModule.addEventListener(WindowEvent.SHOW, this.onWindowShow);
                this._rebateModule.addEventListener(WindowEvent.CLOSE, this.onWindowClose);
            }
            return this._rebateModule;
        }// end function

        private function onWindowShow(event:Event) : void
        {
            this.addEvents(true);
            return;
        }// end function

        private function onWindowClose(event:Event) : void
        {
            this.addEvents(false);
            return;
        }// end function

        private function addEvents(param1:Boolean) : void
        {
            EventUtil.addNetEvent(ServerCommand.RebateDayChargeInfoBack, this.onDayRechargeInfo, param1);
            EventUtil.addSelfEvent(EventName.RebateDayRechargeGoToRecharge, this.onGotoRechargeHd, param1);
            return;
        }// end function

        private function onOpenRechargeModule(param1:Object) : void
        {
            view.show();
            return;
        }// end function

        private function onDayRechargeInfo(param1:Object) : void
        {
            var _loc_2:int = 0;
            if (this._rebateModule && this._rebateModule.curPanel && this._rebateModule.curPanel is RebateDayRechargePanel)
            {
                _loc_2 = param1 as int;
                (this._rebateModule.curPanel as RebateDayRechargePanel).setData();
            }
            return;
        }// end function

        private function onDateChange(param1:Object) : void
        {
            RechargeDailyUtil.hasGetDic = new Dictionary();
            var _loc_2:* = ClockManager.instance.getDelayDate(1000);
            ClockManager.instance.addDateCall(_loc_2, this.refleshDate);
            return;
        }// end function

        private function refleshDate() : void
        {
            this.updateFirstRechargeInfo(null);
            return;
        }// end function

        private function updateFirstRechargeInfo(param1:Object) : void
        {
            var _loc_2:* = Cache.instance.firstRecharge.firstRechargeRewardInfo;
            var _loc_3:* = ClockManager.instance.serverOpenDateNum;
            if (_loc_2)
            {
                if (Cache.instance.rebate.dayRechargeInfo && Cache.instance.rebate.dayRechargeInfo.recharge > 0 && _loc_2.isGet == 1 && RechargeDailyUtil.canShow())
                {
                    FirstRechargeDay500Icon.instance.show();
                    if (RechargeDailyUtil.hasRw())
                    {
                        FirstRechargeDay500Icon.instance.playEffect();
                    }
                    else
                    {
                        FirstRechargeDay500Icon.instance.stopEffect();
                    }
                }
                else if (FirstRechargeDay500Icon.hasInstance())
                {
                    FirstRechargeDay500Icon.instance.hide();
                }
                if (RechargeDailyTotalModule.hasInstance && RechargeDailyTotalModule.instance.isHide == false)
                {
                    RechargeDailyTotalModule.instance.onFlashRW(null);
                }
            }
            return;
        }// end function

        private function onDayRechargeGetHd(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            GameProxy.role.getRechargeTargetReward(_loc_2);
            return;
        }// end function

        private function onGetDayRechargeBackHd(param1:Object) : void
        {
            var _loc_2:int = 0;
            Dispatcher.dispatchEvent(new DataEvent(EventName.RebateShowRewardIconEffect));
            Dispatcher.dispatchEvent(new DataEvent(EventName.RebateShowRewardTabEffect));
            if (this._rebateModule && this._rebateModule.curPanel && this._rebateModule.curPanel is RebateDayRechargePanel)
            {
                _loc_2 = param1 as int;
                (this._rebateModule.curPanel as RebateDayRechargePanel).getRewardBack(_loc_2);
            }
            if (this._rebateModule && GameController.rebate.hasDayRecharge() == false)
            {
                this._rebateModule.hide();
            }
            if (FirstRechargeDay500Module.hasInstance && FirstRechargeDay500Module.instance.isHide == false)
            {
                if (FirstRechargeDay500Module.hasRw() == false)
                {
                    FirstRechargeDay500Module.instance.hide();
                }
                else
                {
                    FirstRechargeDay500Module.instance.updateBtn();
                }
            }
            if (FirstRechargeDay500Icon.hasInstance() && FirstRechargeDay500Icon.instance.isHide == false)
            {
                if (RechargeDailyUtil.hasRw() == false)
                {
                    FirstRechargeDay500Icon.instance.hide();
                }
            }
            return;
        }// end function

        private function onGotoRechargeHd(event:DataEvent) : void
        {
            GameUtil.gotoRecharge(ModuleTypeConst.RebateRecharge);
            return;
        }// end function

    }
}
